package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketCollect;
import com.cskaoyan.bean.po.MarketCollectExample;
import com.cskaoyan.bean.usersadmin.CollectData;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: CollectServiceImpl
 * Description:
 * date: 2022/6/5 19:55
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Override
    public CollectData query(BaseParam param, Integer userId, Integer valueId) {
        //开启分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        //排序
        example.setOrderByClause("add_time desc");
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        //获得分页信息
        PageInfo pageInfo = new PageInfo(marketCollects);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CollectData.data(total,pages,param.getLimit(),param.getPage(),marketCollects);
    }
}
